home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / font.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  5KB  |  160 lines

  1. /*
  2.  * Copyright (c) 1993-94, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. #include <GL/glx.h>
  23. #include <GL/gl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. /* prototype */
  29. void makeRasterFont(Display **dpy);
  30.  
  31. static long W = 300, H = 300;
  32. static int attributeList[] = {GLX_RGBA, None}; 
  33. static Bool WaitForNotify (Display *d, XEvent *e, char *arg)
  34. {
  35.     return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
  36. }
  37.  
  38. GLuint base;
  39.  
  40. void makeRasterFont(Display **dpy)
  41. {
  42.     XFontStruct *fontInfo;
  43.     Font id;
  44.     unsigned int first, last;
  45.  
  46.     fontInfo = XLoadQueryFont(*dpy,
  47.       "-adobe-courier-bold-o-normal--20-140-100-100-m-110-iso8859-1");
  48.     if (fontInfo == NULL) {
  49.     printf("no font found\n");
  50.     exit(0);
  51.     }
  52.     id = fontInfo->fid;
  53.     first = fontInfo->min_char_or_byte2;
  54.     last = fontInfo->max_char_or_byte2;
  55.     base = glGenLists(last+1);
  56.     if (base == 0) {
  57.     printf("out of display lists\n");
  58.     exit(0);
  59.     }
  60.     glXUseXFont(id, first, last-first+1, base+first);
  61. }
  62.  
  63. void printString(char *s)
  64. {
  65.     glPushAttrib(GL_LIST_BIT);
  66.     glListBase(base);
  67.     glCallLists(strlen(s), GL_UNSIGNED_BYTE, (unsigned char *)s);
  68.     glPopAttrib();
  69. }
  70.  
  71. void gldrawing(void) {
  72.     glLoadIdentity();
  73.     glOrtho(-500.0, 500.0, -500.0, 500.0, 0.0, 1.0);
  74.     glClearColor(0.0, 0.0, 0.0, 0.0);
  75.  
  76.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  77.  
  78.     glColor3f(0.9, 0.3, 0.9);
  79.     glBegin(GL_POLYGON);
  80.     glVertex3f( 0.0, -100.0, 0.0);
  81.     glVertex3f( 0.0,  100.0, 0.0);
  82.     glVertex3f(-200.0,  100.0, 0.0);
  83.     glVertex3f(-200.0, -100.0, 0.0);
  84.     glEnd();
  85.     glColor3f(0.9, 0.9, 0.9);
  86.     glRasterPos2f(-100.0, 0.0);
  87.     printString("Here's the origin.");
  88.     glRasterPos2f(-200.0, 102.0);
  89.     printString("HERE'S THE UPPER LEFT CORNER.");
  90.  
  91.     glFlush();
  92. }
  93.  
  94. main(int argc, char **argv)
  95. {
  96.     Display *dpy;                 /* X Display */
  97.     XColor acolor;              /* Color definitions */
  98.     XVisualInfo *vi;              /* X Visual  */
  99.     Colormap cmap;                /* X Colormap */
  100.     XSetWindowAttributes swa;     /* X Window Attributes */
  101.     Window win;                   /* X Window Id */
  102.     GLXContext cx;                /* Extesion to X server; GLX context
  103.                      Data structure for X to hold GL global
  104.                      data, so that we don't have to send them
  105.                      to X through protocal everytime to
  106.                      eliminate X traffic */
  107.     XEvent event, report;         /* X event             */
  108.     int value;
  109.  
  110.                                   /* Standard code to open a X window */
  111.     dpy = XOpenDisplay (0);
  112.     XSynchronize(dpy, True);
  113.                                   /* get a appropriate visual */
  114.     vi = glXChooseVisual (dpy, DefaultScreen(dpy), attributeList);
  115.  
  116.                                       /* create a GLX context */
  117.     cx = glXCreateContext (dpy, vi, None, GL_FALSE);
  118.     
  119.                                   /* create a colormap using this visual */
  120.     cmap = XCreateColormap (dpy, RootWindow(dpy, vi->screen), vi->visual,
  121.                 AllocNone);
  122.  
  123.                                   /* create a X Window */
  124.     swa.colormap = cmap;
  125.     swa.border_pixel = 0;
  126.     swa.event_mask = StructureNotifyMask | KeyPressMask;
  127.     win = XCreateWindow (dpy, RootWindow(dpy,vi->screen), 0, 0, 500, 500,
  128.              0, vi->depth, InputOutput, vi->visual,
  129.              CWBorderPixel|CWColormap|CWEventMask, &swa);
  130.  
  131.     XSelectInput(dpy, win, ExposureMask | KeyPressMask |
  132.                                ButtonPressMask | StructureNotifyMask);
  133.     XMapWindow (dpy,win);
  134.     /* wait for window to map */
  135.     XIfEvent (dpy, &event,WaitForNotify, (char *)win);
  136.  
  137.     /* connect the context to the window */
  138.     if (!glXMakeCurrent(dpy,win,cx) == GL_TRUE) {
  139.     return 0;
  140.     }
  141.     /* make font */
  142.     makeRasterFont(&dpy);
  143.  
  144.     while (1) {
  145.     XNextEvent(dpy, &report);
  146.     switch (report.type) {
  147.         case KeyPress:
  148.         XCloseDisplay(dpy);
  149.         exit(1);
  150.         break;
  151.         case Expose:
  152.         gldrawing();
  153.         break;
  154.         default:
  155.         break;
  156.     } /*end switch*/
  157.     } /*end while*/
  158. }
  159.  
  160.